package com.ruoyi.interview.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.interview.mapper.InterviewReportMapper;
import com.ruoyi.interview.mapper.InterviewRecordMapper;
import com.ruoyi.interview.mapper.InterviewDimensionScoreMapper;
import com.ruoyi.interview.mapper.InterviewDimensionMapper;
import com.ruoyi.interview.mapper.InterviewPositionMapper;
import com.ruoyi.interview.domain.InterviewReport;
import com.ruoyi.interview.domain.InterviewRecord;
import com.ruoyi.interview.domain.InterviewDimensionScore;
import com.ruoyi.interview.domain.InterviewDimension;
import com.ruoyi.interview.domain.InterviewPosition;
import com.ruoyi.interview.domain.vo.InterviewReportDetailVO;
import com.ruoyi.interview.domain.vo.InterviewReportListVO;
import com.ruoyi.interview.service.IInterviewReportService;
import org.springframework.beans.BeanUtils;

/**
 * 面试报告Service业务层处理
 * 
 * @author lishugen
 * @date 2025-06-12
 */
@Service
public class InterviewReportServiceImpl implements IInterviewReportService 
{
    @Autowired
    private InterviewReportMapper interviewReportMapper;

    @Autowired
    private InterviewRecordMapper interviewRecordMapper;

    @Autowired
    private InterviewDimensionScoreMapper interviewDimensionScoreMapper;

    @Autowired
    private InterviewDimensionMapper interviewDimensionMapper;

    @Autowired
    private InterviewPositionMapper interviewPositionMapper;

    /**
     * 查询面试报告
     * 
     * @param id 面试报告主键
     * @return 面试报告
     */
    @Override
    public InterviewReport selectInterviewReportById(Long id)
    {
        return interviewReportMapper.selectInterviewReportById(id);
    }

    /**
     * 查询面试报告列表（包含岗位信息）
     * 
     * @param userId 用户ID
     * @return 面试报告列表VO
     */
    @Override
    public List<InterviewReportListVO> selectInterviewReportList(Long userId)
    {
        return interviewReportMapper.selectInterviewReportList(userId);
    }

    /**
     * 新增面试报告
     * 
     * @param interviewReport 面试报告
     * @return 结果
     */
    @Override
    public int insertInterviewReport(InterviewReport interviewReport)
    {
        interviewReport.setCreateTime(DateUtils.getNowDate());
        return interviewReportMapper.insertInterviewReport(interviewReport);
    }

    /**
     * 修改面试报告
     * 
     * @param interviewReport 面试报告
     * @return 结果
     */
    @Override
    public int updateInterviewReport(InterviewReport interviewReport)
    {
        interviewReport.setUpdateTime(DateUtils.getNowDate());
        return interviewReportMapper.updateInterviewReport(interviewReport);
    }

    /**
     * 批量删除面试报告
     * 
     * @param ids 需要删除的面试报告主键
     * @return 结果
     */
    @Override
    public int deleteInterviewReportByIds(Long[] ids)
    {
        return interviewReportMapper.deleteInterviewReportByIds(ids);
    }

    /**
     * 删除面试报告信息
     * 
     * @param id 面试报告主键
     * @return 结果
     */
    @Override
    public int deleteInterviewReportById(Long id)
    {
        return interviewReportMapper.deleteInterviewReportById(id);
    }

    /**
     * 查询面试报告详情（包含岗位信息和维度评分）
     * 
     * @param id 面试报告主键
     * @return 面试报告详情VO
     */
    @Override
    public InterviewReportDetailVO selectInterviewReportDetailById(Long id) {
        // 1. 查询报告基础信息
        InterviewReport report = interviewReportMapper.selectInterviewReportById(id);
        if (report == null) {
            return null;
        }

        // 2. 创建VO对象并复制属性
        InterviewReportDetailVO detailVO = new InterviewReportDetailVO();
        BeanUtils.copyProperties(report, detailVO);

        // 3. 查询关联的面试记录，获取岗位ID
        InterviewRecord record = interviewRecordMapper.selectInterviewRecordById(report.getRecordId());
        if (record != null && record.getPositionId() != null) {
            // 4. 查询岗位信息
            InterviewPosition position = interviewPositionMapper.selectInterviewPositionById(record.getPositionId());
            if (position != null) {
                detailVO.setPositionName(position.getName());
            }

            // 5. 查询维度评分列表
            InterviewDimensionScore scoreQuery = new InterviewDimensionScore();
            scoreQuery.setReportId(id);
            List<InterviewDimensionScore> scores = interviewDimensionScoreMapper.selectInterviewDimensionScoreList(scoreQuery);

            // 6. 组装维度评分VO，包含维度名称
            List<InterviewReportDetailVO.DimensionScoreVO> dimensionScoreVOs = new ArrayList<>();
            for (InterviewDimensionScore score : scores) {
                InterviewReportDetailVO.DimensionScoreVO scoreVO = new InterviewReportDetailVO.DimensionScoreVO();
                BeanUtils.copyProperties(score, scoreVO);

                // 查询维度信息
                if (score.getDimensionId() != null) {
                    InterviewDimension dimension = interviewDimensionMapper.selectInterviewDimensionById(score.getDimensionId());
                    if (dimension != null) {
                        scoreVO.setDimensionName(dimension.getName());
                        if (dimension.getWeight() != null) {
                            scoreVO.setDimensionWeight(dimension.getWeight().toString());
                        }
                    }
                }
                dimensionScoreVOs.add(scoreVO);
            }
            detailVO.setDimensionScores(dimensionScoreVOs);
        }

        return detailVO;
    }
}
